package com.itheima.web.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.result.PageResult;
import com.itheima.service.sms.SmsHomeBrandService;
import com.itheima.vo.sms.SmsHomeBrand;
import com.itheima.web.mapper.sms.SmsHomeBrandMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsHomeBrandServiceImpl extends ServiceImpl<SmsHomeBrandMapper, SmsHomeBrand> implements SmsHomeBrandService {

    @Override
    public void create(SmsHomeBrand smsHomeBrand) {
         this.save(smsHomeBrand);

    }



    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    @Override
    public PageResult selectByPage(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page<SmsHomeBrand> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(brandName != null, SmsHomeBrand::getBrandName, brandName);
        lqw.eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        this.page(pageInfo,lqw);

        return PageResult.init(pageInfo.getTotal(),Long.valueOf(pageSize),Long.valueOf(pageNum),pageInfo.getRecords());
    }

    @Override
    public void updateStatus(List<Long> ids, Integer recommendStatus) {
        /*LambdaQueryWrapper<SmsHomeBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SmsHomeBrand::getId, ids);
        List<SmsHomeBrand> list = this.list(queryWrapper);
         list = list.stream().map(item -> {
            item.setRecommendStatus(recommendStatus);
            return item;
        }).collect(Collectors.toList());*/
        LambdaUpdateWrapper<SmsHomeBrand> luw = new LambdaUpdateWrapper<>();
        luw.set(SmsHomeBrand::getRecommendStatus, recommendStatus).in(SmsHomeBrand::getId, ids);
        this.update(luw);
    }

    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<SmsHomeBrand> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsHomeBrand::getId, id).set(SmsHomeBrand::getSort, sort);
        this.update(luw);

    }
}
